package com.java.wmovie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.wmovie.entity.Admin;
import com.java.wmovie.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.util.Password;

import javax.servlet.http.HttpSession;

@Controller
public class ALoginController {
    @Autowired
    AdminMapper adminMapper;

    @RequestMapping("/ALogin")
    public String ALogin(){
        return "/admin/ALogin";
    }

    @RequestMapping("/ALogins")
    public  String ALogins(Model model, @RequestParam("a_name")String a_name, @RequestParam("a_password")String a_password, HttpSession session){
        Admin admin=adminMapper.selectOne(new QueryWrapper<Admin>().eq("a_name",a_name)
                .eq("a_password",a_password));
        if(ObjectUtils.isEmpty(admin)){
            model.addAttribute("msg","账号或密码错误");
            session.setAttribute("loginAdmin",null);
            return "/admin/ALogin";
        }else {
            session.setAttribute("loginAdmin",a_name);
            return "redirect:/admin/index";
        }
    }

    @RequestMapping("/ALogout")
    public String ALogout(HttpSession session){
        session.setAttribute("loginAdmin",null);
        return "redirect:/ALogin";
    }

}
